﻿using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Forms;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using AMPLibrary;
using AMPLibrary.Collections;
using AMPLibrary.Controllers;

namespace MediaLibrary.Controls
{
    /// <summary>
    /// Interaction logic for WatchFolderPage.xaml
    /// </summary>
    public partial class WatchFolderPage : Page
    {
        WatchFolderController watchFolderController = WatchFolderController.GetWatchFolderController();

        public WatchFolderPage()
        {
            InitializeComponent();
        
            BindContent();
            watchFolderModeComboBox.SelectedIndex = -1;
            watchFolderModeComboBox.SelectedIndex = (int)Settings.WatchFolderScanMode;
        }
        
        /// <summary>
        /// 
        /// </summary>
        private void BindContent()
        {
            musicListBox.Items.Clear();
            videoListBox.Items.Clear();

            SplittedArrayList wfal = Settings.MusicWatchFolders;

            foreach (string musicWatchFolder in wfal)
            {
                ListBoxItem lbi = new ListBoxItem();
                lbi.Content = musicWatchFolder;

                musicListBox.Items.Add(lbi);
                musicListBox.Items.Refresh();
            }

            wfal = Settings.VideoWatchFolders;

            foreach (string videoWatchFolder in wfal)
            {
                ListBoxItem lbi = new ListBoxItem();
                lbi.Content = videoWatchFolder;

                videoListBox.Items.Add(lbi);
                videoListBox.Items.Refresh();
            }

            timeIntervalTextBox.Text = Settings.WatchFolderScanInterval.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void watchFolderModeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            switch (this.watchFolderModeComboBox.SelectedIndex)
            {
                case 0:
                    this.watchFolderGrid.IsEnabled = false;
                    this.watchFolderModeComboBox.SelectedIndex = 0;
                    Settings.WatchFolderScanMode = WatchFolderScanModes.DoNotScan;
                    watchFolderController.Stop();
                    break;

                case 1:
                    this.watchFolderGrid.IsEnabled = false;
                    this.watchFolderModeComboBox.SelectedIndex = 1;
                    Settings.WatchFolderScanMode = WatchFolderScanModes.FullComputerScan;
                    break;

                case 2:
                    this.watchFolderGrid.IsEnabled = true;
                    this.watchFolderModeComboBox.SelectedIndex = 2;
                    Settings.WatchFolderScanMode = WatchFolderScanModes.WatchFolderScan;
                    break;
            }

            watchFolderController.Start(Settings.WatchFolderScanMode);   
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addMusicWatchFolderButton_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();

            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
                Settings.MusicWatchFolders.Add(folderBrowserDialog.SelectedPath);

            BindContent();

            watchFolderController.Start(Settings.WatchFolderScanMode);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addVideoWatchFolderButton_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();

            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
                Settings.VideoWatchFolders.Add(folderBrowserDialog.SelectedPath);

            BindContent();

            watchFolderController.Start(Settings.WatchFolderScanMode);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void saveItemIntervalButton_Click(object sender, RoutedEventArgs e)
        {
            int timePeriod = 0;

            if (!int.TryParse(timeIntervalTextBox.Text, out timePeriod))
            {
                System.Windows.MessageBox.Show("The amount entered was invalid. Defaulting to 60 mintues.", "Error");
                timeIntervalTextBox.Text = "60";
                Settings.WatchFolderScanInterval = 60;
                return;
            }
            else
                Settings.WatchFolderScanInterval = timePeriod;

            BindContent();

            watchFolderController.Start(Settings.WatchFolderScanMode);         
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void musicListBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Delete)
            {
                Collection<object> selectedItems = new Collection<object>();
                SplittedArrayList wfal = Settings.MusicWatchFolders;

                foreach (ListBoxItem item in musicListBox.SelectedItems)
                {
                    selectedItems.AddItem(item);
                    wfal.Remove(item.Content.ToString());
                }

                foreach (object item in selectedItems)
                    musicListBox.Items.Remove(item);


                BindContent();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void videoListBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Delete)
            {
                Collection<object> selectedItems = new Collection<object>();
                SplittedArrayList wfal = Settings.VideoWatchFolders;

                foreach (ListBoxItem item in videoListBox.SelectedItems)
                {
                    selectedItems.AddItem(item);
                    wfal.Remove(item.Content.ToString());
                }

                foreach (object item in selectedItems)
                    videoListBox.Items.Remove(item);


                BindContent();
            }
        }
    }
}
